#include "cmddailytask.h"

#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "commmsg.h"


CT_REGISTER_ASYNCCMD(ID_MAIN_GET_DAILY_TASK, CCmdGetDailyTask);

int CCmdGetDailyTask::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_GET_DAILY_TASK||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_GET_DAILY_TASK,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("get DailyTask GID: %u", uiGID));

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdGetDailyTask::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    PREPARE_PLAYER_SNS_DETAIL_INFO(this, uiGID);
    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user player base info error, uid = %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    int iGidIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("CCmdGetDailyTask info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }

    //取得用户每日统计数据
    iIndex = pstMainCtrl->m_stPlayerSnsDetailInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user sns detail info error, uid = %u", uiGID));
        return Done(RET_SYSTEM_FAIL);
    }
    CPlayerSnsDetailInfo& stPlayerSnsDetailInfo = pstMainCtrl->m_stPlayerSnsDetailInfoHash[iIndex];
    CPlayerStatic& stPlayerStatic = stPlayerSnsDetailInfo._stMyInfo._stPlayerStatic;

    //CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];
    CUserDailyTask& stUserDailyTask = pstMainCtrl->m_stPlayerOnlineDataHash[iGidIndex]._stUserDailyTask;

    int iNowTime = pstMainCtrl->m_iNow;
    stAns.set_dailyexp(         stPlayerStatic._stTodayRoleGetExp.Get(iNowTime)                 );
    stAns.set_dailyreputation(  stPlayerStatic._stTodayGetReputation.Get(iNowTime)              );
    stAns.set_dailysilver(      stPlayerStatic._stTodayGetSilver.Get(iNowTime)                  );
    stAns.set_dailycontribution(stPlayerStatic._stTodayContribution.Get(iNowTime)               );
    
    pstMainCtrl->m_stDailyTaskMgr.GetTaskList(stUserDailyTask, stAns);

    return AnsClients(RET_OK);
}

int CCmdGetDailyTask::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( iRet );

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("dailytask enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

//////////////////////////////////////////////////////////////////////////
CT_REGISTER_ASYNCCMD(ID_MAIN_FIND_YESTERDAY_TASK, CCmdFindYesterdayTask);

int CCmdFindYesterdayTask::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_FIND_YESTERDAY_TASK ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_FIND_YESTERDAY_TASK,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("commit defend info:[%u]", uiGID));

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdFindYesterdayTask::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    PREPARE_PLAYER_SNS_DETAIL_INFO(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user player base info error, uid = %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    int iGidIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("CCmdGetDailyTask info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    //CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];
    CUserDailyTask& stUserDailyTask = pstMainCtrl->m_stPlayerOnlineDataHash[iGidIndex]._stUserDailyTask;
    
    int iTaskId = stReq.task_id();
    iRet = pstMainCtrl->m_stDailyTaskMgr.FindYesterdayTask(stUserDailyTask, stPlayerBaseInfo, iTaskId);

    stAns.set_gid( uiGID );
    stAns.set_task_id( iTaskId );

    //保存数据
    stUserDailyTask.GetData( stPlayerBaseInfo._stUserDailyTaskData );

    CVarObjectKey stKey;
    stKey.m_chDataType = DT_PLAYER_BASE_INFO;
    stKey.m_u64ID = stPlayerBaseInfo._uiGID;
    CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerBaseInfo, VOP_UPDATE);

    return AnsClients(iRet);
}

int CCmdFindYesterdayTask::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( iRet );

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    stAns.set_ret( iRet );

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("dailytask enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}
/////////////////////////////////////////////////////////////////////
CT_REGISTER_ASYNCCMD(ID_MAIN_OPEN_TBOX, CCmdOpenTBox);

int CCmdOpenTBox::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_OPEN_TBOX ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_OPEN_TBOX,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("open daily task tbox gid:[%u]", uiGID));

    //检查登录
    if (pstMainCtrl->CheckLogin(uiGID, stEnpHead)!=RET_OK)
    {
        return AnsClients(RET_NOT_LOGIN);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdOpenTBox::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    PREPARE_PLAYER_SNS_DETAIL_INFO(this, uiGID);
    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        CT_ERROR(("get user player base info error, uid = %u", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    if (stPlayerBaseInfo._ucIsInited != 1)
    {
        return AnsClients(ERROR_NOT_INITED);
    }

    int iGidIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("CCmdGetDailyTask info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    //CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];
    CUserDailyTask& stUserDailyTask = pstMainCtrl->m_stPlayerOnlineDataHash[iGidIndex]._stUserDailyTask;
    
    int iReqType = stReq.req_type();
    int iFucklyBoxId = 0;  //试试手运双倍奖励宝箱id
    if( iReqType==0 ) //开启宝箱
    {
        int tbox_id = stReq.tbox_id();
        iRet = pstMainCtrl->m_stDailyTaskMgr.OpenTBox(stUserDailyTask, tbox_id, stAns);
    }
    else if( iReqType==1 ) //购买小秘书
    {
        int iSecretaryStyle = stReq.tbox_id();
        iRet = pstMainCtrl->m_stDailyTaskMgr.BuySecretaryStyle(stUserDailyTask, iSecretaryStyle);
    }
    else if( iReqType==2 ) //试试手运
    {
        iRet = pstMainCtrl->m_stDailyTaskMgr.DiyLuckDoubleAwardBox(stUserDailyTask, iFucklyBoxId);
    }

    stAns.set_gid( uiGID );
    stAns.set_req_type( iReqType );

    if( iReqType==2 ) //试试手运
    {
        stAns.set_tbox_id( iFucklyBoxId );
    }
    else
    {
        stAns.set_tbox_id( stReq.tbox_id() );
    }
    //开启成功，给予奖励,是否要用协议发送给前台提示奖品
    //to do

    //保存数据
    stUserDailyTask.GetData( stPlayerBaseInfo._stUserDailyTaskData );

    CVarObjectKey stKey;
    stKey.m_chDataType = DT_PLAYER_BASE_INFO;
    stKey.m_u64ID = stPlayerBaseInfo._uiGID;
    CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerBaseInfo, VOP_UPDATE);
        
    return AnsClients(iRet);
}

int CCmdOpenTBox::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( iRet );

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    stAns.set_ret( iRet );

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("dailytask enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}
